	
	var city = "";
	var urlSafeCity = "";
	var state = "";
	var locationStr = " ";
	var dateAndTime = " ";
	var hasEnteredLocation = false;
	var isZipCode = true;
	
	var itemDescription;
	var itemSeverity;
	var itemReportedDate;
	var itemEndDate;
	var itemImageURL;
		
	var locationTextInput = new TextInput();
	
	locationTextInput.onDone = function() {
		captureInput( this.value );
		if( hasEnteredLocation == true ) {
			main.mainLocationEntry.enteredLocation.value = locationStr;
			main.mainHeader.children[ 1 ].value = locationStr;
			fetchTrafficInfo();
			show(main);
		}
		else {
			alert( "Please enter a city and state separated by a comma." );
			show( locationTextInput );
		}
	};
	
	locationTextInput.onCancel = function() {
		show(main);
	};
		
		
	// this function analyzes and validates the user input.  It tries to extract a 
	// zip code, and if it cannot, tries to extract a city and state.  The Moblet will use 
	// either one of these in the request url to get traffic information.
	function captureInput( someText )
	{
		var len = someText.length;
		var j = 0;
		var c;
		
		isZipCode = true;
		
		// test for a zipcode. 
		if( len == 5 ) {
			for( j = 0; j < 5; j++ ) {
				c = someText.charAt( j );
				debug( "c is " + c );
				if( c < '0' ) {
					isZipCode = false;
					break;
				}
				if( c > '9' ) {
					isZipCode = false;
					break;
				}
				j = j + 1; // without this, for won't loop
			} // for
			if( isZipCode == true ) {
				hasEnteredLocation = true;
				locationStr = someText;
				return;
			}
		} // if len is 5 characters
		
		// could it be a city and state?
		
		var i = someText.indexOf( "," );
		// if index of comma is the last character... input is incomplete.
		if( i + 1 == len ) {
			hasEnteredLocation = false;
			return;
		}
		
		// if comma is not first character, try to extract city and state
		if( i > 1 ) {
			city = someText.substring( 0, i );
			var tempCity = city;
			
			var space = tempCity.indexOf( ' ' );
			if( space > 0 ) {
				while( space > 0 ) {
					urlSafeCity = tempCity.substring( 0, space ) + "%20";
					tempCity = tempCity.substring( space );
					space = tempCity.indexOf( ' ' );
				} // while
				urlSafeCity = urlSafeCity + tempCity.substring( space + 1 );
			} // if space
			
			state = someText.substring( i + 1 );
			if( state.charAt( 0 ) == ' ' ) {
				state = state.substring( 1 );
			}
			locationStr = city + ", " + state;
			hasEnteredLocation = true;
			isZipCode = false;
		}
		else {
			// comma is first character... input is incomplete
			hasEnteredLocation = false;
		}
	} // captureInput
	
	
	// pull the detail information out of the traffic item and populate the detail screen.
	function detail( whichTrafficItem )
	{	
		itemDescription = whichTrafficItem.description;
		itemSeverity = whichTrafficItem.severity;
		itemReportedDate = extractDateString( whichTrafficItem.reportdate );
		itemEndDate = extractDateString( whichTrafficItem.enddate );
		itemImageURL = whichTrafficItem.imageurl;

		detailScreen.reset();
		show( detailScreen );
	}
	
	// takes a date in the form of an integer value, e.g. 1159794060 and converts it 
	// to someething like Tue Sep 19 23:00:00 2006
	function extractDateString( numericDateValue )
	{
		var date = new Date();
		date.setTime( numericDateValue );
		
		var day = getDayStrForInt( date.getDay() );
		var month = getMonthStrForInt( date.getMonth() );
		var dayOfMonth = date.getDate();
		
		var h = date.getHours();
		if( h == 0 ) 
			h = 12;
			
		var m = date.getMinutes();
		if( m < 10 )
			m = "0" + m;
		
		return day + " " + month + " " + dayOfMonth + " " + h + ":" + m;
	}
	
	function getDayStrForInt( dayInt06 )
	{
		switch( dayInt06 ) {
			case 0 : return "Sun";
			case 1 : return "Mon";
			case 2 : return "Tue";
			case 3 : return "Wed";
			case 4 : return "Thu";
			case 5 : return "Fri";
			case 6 : return "Sat";
		} // switch
		return "Unk";
	} // getDayStrForInt
	
	
	function getMonthStrForInt( month011 )
	{
		switch( month011 ) {
			case 0 : return "Jan";
			case 1 : return "Feb";
			case 2 : return "Mar";
			case 3 : return "Apr";
			case 4 : return "May";
			case 5 : return "Jun";
			case 6 : return "Jul";
			case 7 : return "Aug";
			case 8 : return "Sep";
			case 9 : return "Oct";
			case 10 : return "Nov";
			case 11 : return "Dec";
		} // switch
		return "Unk";
	} // getMonthStrForInt
	
	
	function fetchTrafficInfo() 
	{
		var request = new HttpRequest();
		request.onLoaded = function() { renderTrafficInfo( this.responseXML );	};
		var requestURL = "http://api.local.yahoo.com/MapsService/V1/trafficData?appid=mf_traffic_demo&severity=1&radius=5&include_map=1&image_height=200&image_width=200&";
		
		if( isZipCode == true ) {
			requestURL = requestURL + "zip=" + locationStr;
		}
		else {
			requestURL = requestURL + "city=" + urlSafeCity + "&state=" + state;
		}
			
		request.open( "GET", requestURL );
		request.overrideMimeType( "text/xml" ); // must do this after open
		request.applyFilter( "mTraffic.xsl" );
		request.send();
	} // fetchTrafficInfo
	
	
	// update the time information
	function incrementTimeValue()
	{
		var rightNow = new Date();
		var h = rightNow.getHours();
		var m = rightNow.getMinutes();
		
		if( m < 10 ) {
			m = "0" + m;
		}
		if( h == 0 ) {
			h = "12";
		}
		
		dateAndTime = "Current conditions as of " + h + ":" + m + "PDT";
		main.mainHeader.children[ 0 ].value = dateAndTime;
	} // incrementTimeValue
	
	
	// populate the main screen's traffic conditions prototype with traffic line item data.
	function renderTrafficInfo( xmlResult )
	{			
		main.mainConditions.children.clear();
		main.reset;
		
		var trafficItems  = xpath(xmlResult#/result/item);
		debug( trafficItems.length + " traffic items found." ); 
		
		if( trafficItems.length == 0 ) {
			return;
		}
		
		for( anItem in trafficItems ) {
			var lineItem = new trafficItem(); // trafficItem is a prototype defined in the mil file.
			  
			//debug( anItem.title.nodeText );
			//debug( anItem.desc.nodeText );
			
			lineItem.title =  anItem.title.nodeText;
			lineItem.description = anItem.desc.nodeText;
			lineItem.severity = anItem.severity.nodeText;
			lineItem.reportdate = anItem.reportdate.nodeText;
			lineItem.enddate = anItem.enddate.nodeText;
			lineItem.imageurl = anItem.imageurl.nodeText;
			
			main.mainConditions.children.push( lineItem );
		}
	} // renderTrafficInfo
	